import time
from math import trunc

import requests
import json, execjs
from openpyxl.workbook import Workbook

jsCode = execjs.compile(open('emo.js', 'r', encoding='utf-8').read()).call

if __name__ == "__main__":
    page = 1
    pageSize = 100

    headers = {
        "authority": "hz.zczy56.com",
        "method": "POST",
        "path": "/oms-consignor-pc/order/source/queryBatchGoodsSource",
        "scheme": "https",
        "accept": "application/json, text/plain, */*",
        "accept-encoding": "gzip, deflate, br, zstd",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "aiossotokenid": "19b2179a-454e-48a7-9a75-545eee3c1199",
        "cache-control": "no-cache",
        "content-length": "573",
        "content-type": "application/json;charset=UTF-8",
        "cookie": "acw_tc=0b32825a17267076947061366e05c05a26d60c5d511e089c0283155836e091",
        "origin": "https://hz.zczy56.com",
        "pragma": "no-cache",
        "priority": "u=1, i",
        "referer": "https://hz.zczy56.com/sourceOfGoods.html",
        "sec-ch-ua": "\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Microsoft Edge\";v=\"128\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
        "x-requested-with": "XMLHttpRequest"
    }
    # 普通
    # url = "https://hz.zczy56.com/oms-consignor-pc/order/source/querySourceOfGoods"

    # 批量
    url = "https://hz.zczy56.com/oms-consignor-pc/order/source/queryBatchGoodsSource"

    # 线下
    # url = "https://hz.zczy56.com/oms-consignor-pc/order/source/querySourceOfTenderGoods"

    wb = Workbook()
    ws = wb.active

    # 写入表头
    header = ["起点", "终点", "运程(km)", "增值服务运费(%)", "货品", "货物备注", "装货开始时间", "装货结束时间",
              "价格(含税)"]
    ws.append(header)

    # 循环标志
    exit_loop = True

    while exit_loop:
        data = {
            "encryParam": jsCode('Encrypt', page, pageSize)
        }
        data = json.dumps(data, separators=(',', ':'))
        response = requests.post(url, headers=headers, data=data)

        # 转为json数据
        list_data = response.json()


        if len(list_data['data']['rootArray']) == 0:
            print("结束!!!")
            exit_loop = False
        else:
            # print(list_data)
            print(len(list_data['data']['rootArray']),)
            print(list_data['code'])
            print(list_data['msg'])
            # print(list_data['data'])
            if 'nowPage' in list_data['data']:
                print('nowPage:', list_data['data']['nowPage'], 'pageSize:', list_data['data']['pageSize'], 'totalSize:', list_data['data']['totalSize'], 'totalPage:', list_data['data']['totalPage'], 'totalSize:', list_data['data']['totalSize'])


            json_data = list_data['data']['rootArray']
            # print(json_data)

            for row in json_data:
                despatch = row['despatchPro'] + row['despatchDis']
                deliver = row['deliverPro'] + row['deliverCityStr']
                distance = row['distance']

                if 'advanceRatio' in row:
                    advanceRatio = row['advanceRatio']
                else:
                    advanceRatio = '无'

                goodsDescription = row['goodsDescription']

                if 'cargoRemark' in row:
                    cargoRemark = row['cargoRemark']
                else:
                    cargoRemark = '无'

                despatchStart = row['despatchStart']
                despatchEnd = row['despatchEnd']
                if "price" in row:
                    if row['freightType'] == '1':
                        price = '单价:' + row['price']
                    else:
                        price = '包车:' + row['price']
                else:
                    price = '无'

                ws.append(
                    [despatch, deliver, distance, advanceRatio, goodsDescription, cargoRemark, despatchStart, despatchEnd,
                     price])

        time.sleep(1)
        page += 1





    wb.save('./批量货源.xlsx')
    print('over!')
